package com.heying.xmlandjson.dom4j;

import com.heying.xmlandjson.entity.Book;
import com.heying.xmlandjson.entity.BookStore;
import com.sun.tools.javac.Main;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class BookStoreXml {

    private Document xmlDoc;

    private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

    public BookStoreXml(){
        try {
            //classpath的绝对路径
            String classpath = ClassLoader.getSystemResource("").getPath();
            File xmlFile = new File(classpath+"bookstore.xml"); //输入输出流中文件对象关联路径
            SAXReader reader = new SAXReader();
            this.xmlDoc = reader.read(xmlFile);//解析文档，(看源代码)
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

//    public BookStore queryBookStore(){
//        return Stream.of(xmlDoc.getRootElement()) .map(
//                bookstoreEl->
//                new BookStore(bookstoreEl.elementText("name"),
//                             bookstoreEl.elementText("address"),
//                             bookstoreEl.element("books").elements().stream().map(bookEl ->
//                                    new Book(
//                                            bookEl.attributeValue("isbn"),
//                                            bookEl.elementText("title"),
//                                            Integer.parseInt(bookEl.elementText("price")),
//                                            bookEl.elementText("author"),
//                                            bookEl.elementText("publisher"),
//                                            bookEl.elementText("pdate"))
//                               ).collect(Collectors.toList()))
//
//        ).findFirst().get();
//    }

    public BookStore queryBookStore(){
        return new BookStore(xmlDoc.getRootElement().elementText("name"),
                xmlDoc.getRootElement().elementText("address"),
                xmlDoc.getRootElement().element("books").elements()
                        .stream()
                        .map(BookStoreXml::mapBook)
                        .collect(Collectors.toList())
        );
    }

    public Book queryBookByIsbn(String isbn){
        return xmlDoc.getRootElement().element("books").elements()
                .stream()
                .filter(bookEl->bookEl.attributeValue("isbn").equals(isbn))
                .map(BookStoreXml::mapBook)
                .findFirst()
                .orElse(null);
    }

    private  static Book mapBook(Element bookEl){
        try {
            return new Book(
                    bookEl.attributeValue("isbn"),
                    bookEl.elementText("title"),
                    Integer.parseInt(bookEl.elementText("price")),
                    bookEl.elementText("author"),
                    bookEl.elementText("publisher"),
                    simpleDateFormat.parse(bookEl.elementText("pdate")));
        } catch (ParseException e) {
            e.printStackTrace();
            throw new RuntimeException("日期格式不正确");
        }

    }

    public static void main(String[] args) {
        BookStoreXml bookStoreXml = new BookStoreXml();
//        bookStoreXml.queryBookStore();
//        System.out.println(bookStoreXml.queryBookStore());
        System.out.println(bookStoreXml.queryBookByIsbn("10001"));
    }

}
